<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 7/18/2016
 * Des: 系统管理员
 */
defined('INHkMvc') or exit('Access Invalid!');

class App_memberModel extends AppModel
{
	/**
	 * 获取token
	 * @param  [type] $member_id [description]
	 * @param  string $fields [description]
	 * @return [type]            [description]
	 */
	public function getOneByMemberId($member_id, $fields = '')
	{
	    return $this->field($fields)->find(['member_id' => intval($member_id)]);
	}
	public function getOneByToken($token, $fields = '')
	{
		$where=[];
	    $where = ['token' => $token];
	    return $this->field($fields)->find($where);
	}
	/**
	 * 获取member
	 */
	public function getMemberByMobile($mobile,$fields=''){
		return $this->field($fields)->find(['mobile' =>$mobile]);
	}
	/**
	 * 注册
	 * @param $mobile
	 * @param $nickname
	 * @param $password
	 * @return bool
	 */
	public function register($mobile, $nickname, $password)
	{
	    $data = self::getRegisterData($mobile, $nickname, $password);
	    $data['short'] = $this->getShort();
	    if (trim($data['nickname']) == '') {
	        $data['nickname'] = '匿名';
	    }
	    return $this->insert($data);
	}

	/**
	 * 获取注册信息
	 * @param $mobile
	 * @param $nickname
	 * @param $password
	 * @return array
	 */
	static protected function getRegisterData($mobile, $nickname, $password)
	{
	    $time = time();
	    $ip = getServerIp();
	    $data = [
	        'mobile' => trim($mobile),
	        'account' => trim($mobile),
	        'nickname' => trim($nickname),
	        'password' => md52(trim($password)),
	        'reg_time' => $time,
	        'last_time' => $time,
	        'reg_ip' => $ip,
	        'last_ip' => $ip,
	        'is_reg' => 1,
	        'route' => 0
	    ];

	    return $data;
	}
	/**
	 * 获取用户短链
	 * @return string
	 */
	public function getShort()
	{
	    $short = createcode(8);
	    if ($this->field('member_id')->find(['short' => $short])) {
	        $this->getShort();
	    }
	    return $short;
	}
	public function getMemberInfo($mobile)
	{
	    $join = [
	        $this->table => ['left',
	            ['member_id', 'member_vip', 'member_vip.member_id']
	        ]
	    ];
	    $where=[];
	    $where = ['mobile' => $mobile];
	    return $this->table()->join($join)->field('member_id,account,headimgurl head_img,nickname nick_name,member_vip.ali_account payment_account,member_vip.mobile mobile,token,member_vip.level vip_level,member_vip.end_time vip_deadline')->find($where);
	}
	public function appLogin($mobile, $md5_password)
	{
	    $res = $this->getMemberByMobile($mobile, 'member_id,password,token,type');
	    $where=[];
	    if ($res['password'] ==$md5_password) {
	        if (intval($res['type']) != 1 || empty($res['token'])) {
	            $token = uniqid(createcode(3));
	            $data['token'] = $token;
	            $where = ['member_id' => intval($res['member_id'])];
	            $res = $this->update($data, $where);
	        }
	        if ($res === false) {
	            return false;
	        } else {
	            $join = [
	                $this->table => ['left',
	                    ['member_id', 'member_vip', 'member_vip.member_id']
	                ]
	            ];
	            $where = ['mobile' => $mobile];
	            return $this->table()->join($join)->field('member_id,account,headimgurl head_img,nickname nick_name,member_vip.ali_account payment_account,member_vip.mobile mobile,token,member_vip.level vip_level,member_vip.end_time vip_deadline')->find($where);
	        }
	    } else {
	        return false;
	    }
	}
	/**
	 * 获取日志登录信息
	 * @param $where
	 * @param $content
	 * @param $error
	 * @param $status
	 * @return mixed
	 */
	public function getMemberLoginLog($where, $content, $error, $status)
	{
	    $res = $this->field('member_id,last_ip,last_time')->find($where);
	    $res['login_ip'] = $res['last_ip'];
	    $res['login_time'] = $res['last_time'];
	    $res['error'] = $error;
	    $res['content'] = $content;
	    $res['status'] = $status;
	    $res['type'] = 0;
	    return $res;
	}
}
